<%--
  Created by IntelliJ IDEA.
  User: legion
  Date: 2020/12/25
  Time: 8:58
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <link href="../css/bootstrap.min.css" rel="stylesheet">
    <script type="text/javascript" src="../js/jquery-3.3.1.min.js"></script>
    <!-- 加载 Bootstrap 的所有 JavaScript 插件。你也可以根据需要只加载单个插件。 -->
    <script src="../js/bootstrap.min.js"></script>
</head>
<style type="text/css">
    .div
    {
        display:none;
        border:1px solid ;
        height:50%;
        width:70%;
        position:absolute;/*让节点脱离文档流,我的理解就是,从页面上浮出来,不再按照文档其它内容布局*/
        top:24%;/*节点脱离了文档流,如果设置位置需要用top和left,right,bottom定位*/
        left:24%;
        z-index:2;/*个人理解为层级关系,由于这个节点要在顶部显示,所以这个值比其余节点的都大*/
        background: white;
    }
    #over
    {
        width: 100%;
        height: 100%;
        opacity:0.3;/*设置背景色透明度,1为完全不透明,IE需要使用filter:alpha(opacity=80);*/
        filter:alpha(opacity=80);
        display: none;
        position:absolute;
        top:0;
        left:0;
        z-index:1;
        background-color: rgb(0,0,0);
    }
</style>
<script>

    function particulars(orderId){
        $("#particularsDiv").show()
         $.ajax({
             url:"http://localhost:8080/salesServlet",
             type:"post",
             data:{type:2,'orderId':orderId},
             success: function (data,status,xhr) {
                   if(data==""){
                       return;
                   }
                   data=JSON.parse(data)
                   $("#name").text(data.name)
                   $("#amount").text(data.amount)
                   $("#price").text(data.price)
                   $("#totalPrice").text(data.totalPrice)
                   $("#costPrice").text(data.cost_price)
                   $("#otherCost").text(data.otherCost)
                   $("#totalCostPrice").text(data.totalCostPrice)
                   $("#royalties").text(data.royalties)
                   $("#userName").text(data.userName)
                   $("#address").text(data.address)
                   $("#phone").text(data.phone)
                   $("#billInfo").text(data.billInfo)
                   $("#invoiceId").text(data.invoiceId)
                   $("#remarks").text(data.remarks)
             }
         })
    }
    /**
     *封装查询全部
     */
    function x2() {
        //查询数据
        $.ajax({
            url:"http://localhost:8080/salesServlet",
            type:"post",
            data:{type:4},
            success: function (data,status,xhr) {
                if(data==""){
                    return;
                }
                data = JSON.parse(data)
                $("#tbody").empty()
                let tr = ""
                for (let i = 0; i <data.length ; i++) {
                    if(data[i].status=="0"){
                        tr+="<tr style='background-color: yellow'><td><input name='radio' type='radio' value='"+data[i].salesId+"'></td><td>"+data[i].salesId+"</td><td>"+data[i].user_id+"</td>"+
                            "<td>"+data[i].orderId+"</td><td>"+data[i].name+"</td>"+
                            "<td>"+data[i].amount+"</td><td>"+data[i].tracking_name+"</td>"+
                            "<td>"+data[i].tracking_number+"</td><td>"+data[i].create_time+"</td><td><input onclick='x1("+data[i].salesId+","+data[i].amount+","+data[i].orderId+")' type='button' class=\"btn btn-info\"   value='收到退货'></td></tr>"
                    }else{
                        tr+="<tr><td><input name='radio' type='radio' value='"+data[i].salesId+"'></td><td>"+data[i].salesId+"</td><td>"+data[i].user_id+"</td>"+
                            "<td>"+data[i].orderId+"</td><td>"+data[i].name+"</td>"+
                            "<td>"+data[i].amount+"</td><td>"+data[i].tracking_name+"</td>"+
                            "<td>"+data[i].tracking_number+"</td><td>"+data[i].create_time+"</td><td><input type='button' class=\"btn btn-info\"   value='已收到退货'></td></tr>"
                    }

                }
                $("#tbody").append(tr)
            }
        })
    }
    /**
     *点击退货显示退货div
     */
    function salesReturn(orderId){
         $("#div").show()
        /**
         * 点击按钮提交退货信息
         */
        $("#submit").click(function () {
            if ($("#trackingNumber").val()=="" || $("#trackingName").val()==""){
                alert("请补全物流信息！")
                return;
            }
            //点击按钮 按钮变成只读状态
            $("#submit").attr("readOnly","true");
            $.ajax({
                url:"http://localhost:8080/salesServlet",
                type:"post",
                data:{type:3,'orderId':orderId,
                    'trackingNumber':$("#trackingNumber").val(),
                    'trackingName':$("#trackingName").val()},
                success: function (data,status,xhr) {
                    //解开按钮只读状态
                    $("#sa input").attr("readOnly",false);
                    $("#trackingNumber").val("")
                    $("#trackingName").val("")
                    $("#div").hide()
                    $("#insertDiv").hide()
                    //刷新数据
                    x2()
                    //刷新分页按钮组
                    $.ajax({
                        url:"http://localhost:8080/salesServlet",
                        type:"post",
                        data:{type:9,'SelectNumber':$("#SelectNumber").val(),
                            'selectName':$("#selectName").val()},
                        success: function (data,status,xhr) {
                            console.log(data+"=========")
                            if(data==""){
                                return;
                            }
                            data = JSON.parse(data)
                            $("#pageDiv").empty()
                            let button = ""
                            for (let i = 1; i <= data.page; i++) {
                                button+="<button onclick='x("+i+")' value='"+i+"' class=\"btn btn-info\">"+i+"</button>"
                            }
                            $("#pageDiv").append(button)
                        }
                    })
                    //刷新订单信息
                    $.ajax({
                        url:"http://localhost:8080/salesServlet",
                        type:"post",
                        data:{type:1},
                        success: function (data,status,xhr) {
                            if(data==""){
                                return;
                            }
                            data = JSON.parse(data)
                            $("#tbody1").empty()
                            var tr=""
                            for(var a=0;a<data.length;a++){
                                tr+="<tr><td>"+data[a].orderId1+"</td><td>"+data[a].name+"</td>"+
                                    "<td>"+data[a].amount+"</td><td>"+data[a].price+"</td>"+
                                    "<td>"+data[a].totalPrice+"</td>"+"<td>"+data[a].cost_price+"</td>"+
                                    "<td>"+data[a].otherCost+"</td>"+"<td>"+data[a].totalCostPrice+"</td>"+
                                    "<td><button onclick='particulars("+data[a].orderId+")'>详情</button> <button onclick='salesReturn("+data[a].orderId+")'>退货</button></td>"+"</tr>"
                            }
                            $("#tbody1").append(tr)
                        }
                    })
                },error:function () {
                    //解开按钮只读状态
                    $("#sa input").attr("readOnly",false);
                }
            })
        })

    }

/**
 * 查询
 */
//获取当前页数 分页查询
function x(page){
    $.ajax({
        url:"http://localhost:8080/salesServlet",
        type:"post",
        data:{type:10,page:page,'SelectNumber':$("#SelectNumber").val(),
            'selectName':$("#selectName").val()},
        success: function (data,status,xhr) {
            if(data==""){
                return;
            }
            data = JSON.parse(data)
            $("#tbody").empty()
            let tr = ""
            for (let i = 0; i <data.length ; i++) {
                if(data[i].status=="0"){
                    tr+="<tr style='background-color: yellow'><td><input name='radio' type='radio' value='"+data[i].salesId+"'></td><td>"+data[i].salesId+"</td><td>"+data[i].user_id+"</td>"+
                        "<td>"+data[i].orderId+"</td><td>"+data[i].name+"</td>"+
                        "<td>"+data[i].amount+"</td><td>"+data[i].tracking_name+"</td>"+
                        "<td>"+data[i].tracking_number+"</td><td>"+data[i].create_time+"</td><td><input onclick='x1("+data[i].salesId+","+data[i].amount+","+data[i].orderId+")' type='button' class=\"btn btn-info\"   value='收到退货'></td></tr>"
                }else{
                    tr+="<tr><td><input name='radio' type='radio' value='"+data[i].salesId+"'></td><td>"+data[i].salesId+"</td><td>"+data[i].user_id+"</td>"+
                        "<td>"+data[i].orderId+"</td><td>"+data[i].name+"</td>"+
                        "<td>"+data[i].amount+"</td><td>"+data[i].tracking_name+"</td>"+
                        "<td>"+data[i].tracking_number+"</td><td>"+data[i].create_time+"</td><td><input type='button' class=\"btn btn-info\"   value='已收到退货'></td></tr>"
                }

            }
            $("#tbody").append(tr)
        }
    })
}
//修改退货状态
function x1(id,amount,orderId){
   $.ajax({
       url:"http://localhost:8080/salesServlet",
       type:"post",
       data:{type:11,id:id,'amount':amount,'orderId':orderId},
       success: function (data,status,xhr) {
           //查询数据
           x2()
       }
   })
}
    $(function () {
        /**
         * 添加分页按钮组
         */
        $.ajax({
            url:"http://localhost:8080/salesServlet",
            type:"post",
            data:{type:9,'SelectNumber':$("#SelectNumber").val(),
                'selectName':$("#selectName").val()},
            success: function (data,status,xhr) {
                console.log(data+"=========")
                if(data==""){
                    return;
                }
                data = JSON.parse(data)
                $("#pageDiv").empty()
                let button = ""
                for (let i = 1; i <= data.page; i++) {
                    button+="<button onclick='x("+i+")' value='"+i+"' class=\"btn btn-info\">"+i+"</button>"
                }
                $("#pageDiv").append(button)
            }
        })
        /**
         * 查询全部退货订单
         */
        x2()
        /**
         * 根据条件查询退货订单
         */
        $("#select").click(function () {
            $.ajax({
                url:"http://localhost:8080/salesServlet",
                type:"post",
                data:{type:5,'SelectNumber':$("#SelectNumber").val(),
                      'selectName':$("#selectName").val()},
                success: function (data,status,xhr) {
                        if(data==""){
                            return;
                        }
                        data = JSON.parse(data)
                        $("#tbody").empty()
                        let tr = ""
                    for (let i = 0; i <data.length ; i++) {
                        if(data[i].status=="0"){
                            tr+="<tr style='background-color: yellow'><td><input name='radio' type='radio' value='"+data[i].salesId+"'></td><td>"+data[i].salesId+"</td><td>"+data[i].user_id+"</td>"+
                                "<td>"+data[i].orderId+"</td><td>"+data[i].name+"</td>"+
                                "<td>"+data[i].amount+"</td><td>"+data[i].tracking_name+"</td>"+
                                "<td>"+data[i].tracking_number+"</td><td>"+data[i].create_time+"</td><td><input onclick='x1("+data[i].salesId+","+data[i].amount+","+data[i].orderId+")' type='button' class=\"btn btn-info\"   value='收到退货'></td></tr>"
                        }else{
                            tr+="<tr><td><input name='radio' type='radio' value='"+data[i].salesId+"'></td><td>"+data[i].salesId+"</td><td>"+data[i].user_id+"</td>"+
                                "<td>"+data[i].orderId+"</td><td>"+data[i].name+"</td>"+
                                "<td>"+data[i].amount+"</td><td>"+data[i].tracking_name+"</td>"+
                                "<td>"+data[i].tracking_number+"</td><td>"+data[i].create_time+"</td><td><input type='button' class=\"btn btn-info\"   value='已收到退货'></td></tr>"
                        }

                    }
                        $("#tbody").append(tr)
                }
            })
            //刷新按钮组
            $.ajax({
                url:"http://localhost:8080/salesServlet",
                type:"post",
                data:{type:9,'SelectNumber':$("#SelectNumber").val(),
                    'selectName':$("#selectName").val()},
                success: function (data,status,xhr) {
                    console.log(data+"=========")
                    if(data==""){
                        return;
                    }
                    data = JSON.parse(data)
                    $("#pageDiv").empty()
                    let button = ""
                    for (let i = 1; i <= data.page; i++) {
                        button+="<button onclick='x("+i+")' value='"+i+"' class=\"btn btn-info\">"+i+"</button>"
                    }
                    $("#pageDiv").append(button)
                }
            })
        })
    })
    /**
     * 修改退货信息
     */
    $(function () {
        $("#update").click(function () {
            var radioValue= $("input[name='radio']:checked").val();//获取当前单选按钮value值
             if(radioValue==undefined){
                 alert("请选择一个退货订单！")
                 return;
             }
              $("#updateDiv").show()
            $.ajax({
                url:"http://localhost:8080/salesServlet",
                type:"post",
                data:{type:7,'id':radioValue},
                success: function (data,status,xhr) {
                    if(data==""){
                        return;
                    }
                    data = JSON.parse(data)
                    $("#trackingNumber1").val(data.tracking_number)
                    $("#trackingName1").val(data.tracking_name)
                }
            })
            $("#updateSubmit").click(function () {
                  $.ajax({
                      url:"http://localhost:8080/salesServlet",
                      type:"post",
                      data:{type:6,'id':radioValue,
                            'trackingNumber':$("#trackingNumber1").val(),
                            'trackingName':$("#trackingName1").val()},
                      success: function (data,status,xhr) {
                          //刷新数据
                          x2()
                          //刷新按钮组
                          $.ajax({
                              url:"http://localhost:8080/salesServlet",
                              type:"post",
                              data:{type:9,'SelectNumber':$("#SelectNumber").val(),
                                  'selectName':$("#selectName").val()},
                              success: function (data,status,xhr) {
                                  console.log(data+"=========")
                                  if(data==""){
                                      return;
                                  }
                                  data = JSON.parse(data)
                                  $("#pageDiv").empty()
                                  let button = ""
                                  for (let i = 1; i <= data.page; i++) {
                                      button+="<button onclick='x("+i+")' value='"+i+"' class=\"btn btn-info\">"+i+"</button>"
                                  }
                                  $("#pageDiv").append(button)
                              }
                          })
                          $("#updateDiv").hide()
                      }
                  })
            })
        })
    })
    /**
     * 删除退货订单
     */
    $(function () {
         $("#delete").click(function () {
             var radioValue= $("input[name='radio']:checked").val();//获取当前单选按钮value值
             if(radioValue==undefined){
                 alert("请选择一个退货订单！")
                 return;
             }
             //刷新数据
             $.ajax({
                 url:"http://localhost:8080/salesServlet",
                 type:"post",
                 data:{type:8,id:radioValue},
                 success: function (data,status,xhr) {
                         //刷新数据
                     x2()
                 }
             })
             //刷新按钮组
             $.ajax({
                 url:"http://localhost:8080/salesServlet",
                 type:"post",
                 data:{type:9,'SelectNumber':$("#SelectNumber").val(),
                     'selectName':$("#selectName").val()},
                 success: function (data,status,xhr) {
                     console.log(data+"=========")
                     if(data==""){
                         return;
                     }
                     data = JSON.parse(data)
                     $("#pageDiv").empty()
                     let button = ""
                     for (let i = 1; i <= data.page; i++) {
                         button+="<button onclick='x("+i+")' value='"+i+"' class=\"btn btn-info\">"+i+"</button>"
                     }
                     $("#pageDiv").append(button)
                 }
             })
         })
    })

    $(function () {
        /**
         * 点击添加按钮 先查询全部订单 显示到页面
         */
        $("#insert").click(function () {
            $("#insertDiv").show()
            $.ajax({
                url:"http://localhost:8080/salesServlet",
                type:"post",
                data:{type:1},
                success: function (data,status,xhr) {
                        if(data==""){
                            return;
                        }
                        data = JSON.parse(data)
                        $("#tbody1").empty()
                        var tr=""
                        for(var a=0;a<data.length;a++){
                            tr+="<tr><td>"+data[a].orderId1+"</td><td>"+data[a].name+"</td>"+
                                "<td>"+data[a].amount+"</td><td>"+data[a].price+"</td>"+
                                "<td>"+data[a].totalPrice+"</td>"+"<td>"+data[a].cost_price+"</td>"+
                                "<td>"+data[a].otherCost+"</td>"+"<td>"+data[a].totalCostPrice+"</td>"+
                                "<td><button onclick='particulars("+data[a].orderId+")'>详情</button> <button onclick='salesReturn("+data[a].orderId+")'>退货</button></td>"+"</tr>"
                        }
                        $("#tbody1").append(tr)
                }
            })
        })

        /**
         * 点击按钮关闭添加页面
         */
        $("#close").click(function () {
                    $("#insertDiv").hide()
            })
        /**
         * 点击关闭订单详情div
         */
        $("#close1").click(function () {
                    $("#particularsDiv").hide()
        })
        /**
         * 点击关闭修改div
         */
        $("#close3").click(function () {
                    $("#updateDiv").hide()
        })
        /**
         * 点击关闭添加div
         */
        $("#close2").click(function () {
                   $("#div").hide()
        })


    })
</script>
<body>
<center>
    <div>物流单号：<input id="SelectNumber"> 产品名称：<input id="selectName"> <input id="select" value="查询" type="button" class="btn btn-info"></div>
    <div><input id="insert" type="button" value="添加" class="btn btn-info"> <input id="update" type="button" value="修改" class="btn btn-info"> <input id="delete" type="button" value="删除" class="btn btn-info"></div>
</center>
<table class="table table-bordered">
    <tr class="table-active">
        <td></td>
        <td>ID</td>
        <td>业务员id</td>
        <td>订单编号</td>
        <td>商品名称</td>
        <td>数量</td>
        <td>物流公司名称</td>
        <td>物流编号</td>
        <td>创建时间</td>
        <td>操作</td>
    </tr>
    <tbody id="tbody">

    </tbody>
</table>
<center>
    <div id="pageDiv">

    </div>
</center>
<div id="insertDiv" class="div" style="height: 65vh;overflow-y: scroll">
    <table class="table table-bordered">
        <tr>
            <td>编号</td>
            <td>商品名称</td>
            <td>数量</td>
            <td>单价</td>
            <td>总价</td>
            <td>成本</td>
            <td>其他成本</td>
            <td>总成本</td>
            <td>操作</td>
        </tr>
        <tbody id="tbody1">

        </tbody>
    </table>
    <center>
        <button id="close" class="btn btn-info">关闭</button>
    </center>
</div>
<!--订单详情的div-->
<div id="particularsDiv" style="width: 500px;height: 65vh;overflow-y: scroll" class="div">
    <table class="table table-bordered">
        <tr><td>商品名称</td> <td id="name"></td></tr>
        <tr><td>数量</td> <td id="amount"></td></tr>
        <tr><td>单价</td> <td id="price"></td></tr>
        <tr><td>总价</td> <td id="totalPrice"></td></tr>
        <tr><td>成本</td> <td id="costPrice"></td></tr>
        <tr><td>其他成本</td> <td id="otherCost"></td></tr>
        <tr><td>总成本</td> <td id="totalCostPrice"></td></tr>
        <tr><td>提成</td> <td id="royalties"></td></tr>
        <tr><td>客户姓名</td> <td id="userName"></td></tr>
        <tr><td>地址</td> <td id="address"></td></tr>
        <tr><td>联系电话</td> <td id="phone"></td></tr>
        <tr><td>开票资料</td> <td id="billInfo"></td></tr>
        <tr><td>库存编号</td> <td id="invoiceId"></td></tr>
        <tr><td>备注</td> <td id="remarks"></td></tr>
    </table>
    <center><button id="close1" class="btn btn-info">关闭</button></center>
</div>
<div class="div" id="div"><!--添加div-->
    <table class="table table-bordered">
        <tr><td>物流单号</td><td><input id="trackingNumber"></td></tr>
        <tr><td>物流公司</td><td><input id="trackingName"></td></tr>
    </table>
    <center>
        <button id="submit" class="btn btn-info">提交</button> <button id="close2" class="btn btn-info">关闭</button>
    </center>
</div>
<div class="div" id="updateDiv"><!--修改div-->
    <table class="table table-bordered">
        <tr><td>退货单号</td> <td><input id="trackingNumber1"></td></tr>
        <tr><td>物流单号</td> <td><input id="trackingName1"></td></tr>
    </table>
    <center>
        <button id="updateSubmit" class="btn btn-info">提交</button> <button id="reset" class="btn btn-info">重置</button> <button class="btn btn-info" id="close3">关闭</button>
    </center>
</div>
</body>
</html>
